package com.xgkj.diyiketang.util;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.xgkj.diyiketang.EatShowApp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author
 * @描述 保存工具类
 */
public class SharedUtils {

	public final static SharedPreferences sp = EatShowApp.getSystemConfigSharedFile();

	public static void putString(String key,String value){
        sp.edit().putString(key,value).commit();
    }
    public static String getString(String key){
        String value = sp.getString(key,"");
        return value;
    }
    public static void putInt(String key,int value){
        sp.edit().putInt(key,value).commit();
    }
    public static int getInt(String key){
        int value = sp.getInt(key,0);
        return value;
    }


    public static void putBoolean(String key,boolean b){
        sp.edit().putBoolean(key,b).commit();
    }
    public static boolean getBoolean(String key){
        return sp.getBoolean(key,false);
    }

    /**
     * @描述 序列化对象
     * @return
     * @throws IOException
     */
    public static String serialize(Object object) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(object);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        Log.e("serial", "serialize str =" + serStr);
        return serStr;
    }

    /**
     * @描述 反序列化对象
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static <T> T deSerialization(String str) throws IOException, ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        @SuppressWarnings("unchecked")
        T t = (T) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return t;
    }

    /**
     * @描述 保存对象--序列化对象并保存到SharedPreferences
     * @param key
     *            ---对象名--LOGIN_BEAN
     * @param object
     * @return
     */
    public static boolean saveObject(String key, Object object) {
        SharedPreferences.Editor edit = sp.edit();
        String strObject = null;
        try {
            strObject = serialize(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(strObject)) {
            // 序列化对象失败
            return false;
        }
        edit.putString(key, strObject);
        edit.commit();
        return true;
    }

    /**
     * @描述 获取对象
     * @param key
     *            --对象名--LOGIN_BEAN
     * @return
     */
    public static <T> T getObject(String key) {
        try {
            return deSerialization(sp.getString(key, ""));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
